package com.springboot.autoconfigure.oss.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OssServiceImplScope implements Scope {
    private Map<String,Object> cache=new ConcurrentHashMap<String,Object>();

    /**
     * 自己管理bean
     * @param name the name of the object to retrieve
     * @param objectFactory the {@link ObjectFactory} to use to create the scoped
     * object if it is not present in the underlying storage mechanism
     * @return
     */
    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        Object object = null;
        if (cache.get(name)!=null)return cache.get(name);
        try {
            object = objectFactory.getObject();
            if (null == object)return null;
        } catch (BeansException e) {
            return null;
//            throw new RuntimeException(e);
        }
        cache.put(name,object);
        return object;
    }

    @Override
    public Object remove(String name) {
        return null;
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {

    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }
}
